package com.aiti.lulian.controller;

import cn.hutool.core.lang.tree.Tree;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.base.core.utils.obj.BeanCopyUtil;
import com.aiti.base.core.utils.stringUtil.StringUtil;
import com.aiti.lulian.dto.SysAreaDto;
import com.aiti.lulian.entity.SysArea;
import com.aiti.lulian.service.ISysAreaService;
import com.aiti.lulian.utils.MapUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chl
 * @since 2022-05-12
 */
@RestController
@RequestMapping("sysArea")
@Api(tags = "sysArea", description = "sysArea")
public class SysAreaController extends WebController {
   protected final Logger logger = LoggerFactory.getLogger( SysAreaController.class);

     @Autowired
     private ISysAreaService sysAreaService;


     @ApiOperation(value="保存")
     @PostMapping("")
     public Message addSysArea(@RequestBody SysAreaDto sysAreaDto) {
         try {
             SysArea sysArea = BeanCopyUtil.convertBean(sysAreaDto, SysArea.class);
             return Success(sysAreaService.save(sysArea));
         }catch (Exception e){
              logger.error("保存异常：===》" + e);
              return Error(e.getMessage());
         }
     }
     @ApiOperation(value="查询所有")
     @GetMapping()
     public Message getSysAreaList() {
        try {
            return Success(sysAreaService.list());
        }catch (Exception e){
            logger.error("查询异常：===》" + e);
            return Error(e.getMessage());
        }
     }
     @ApiOperation(value = "查询列表-分页")
     @GetMapping("sysAreaListPage")
     public Message sysAreaListPage(SysAreaDto sysAreaDto) {
         try {
              Integer pageSize = sysAreaDto.getPageSize();
              Integer pageNum = sysAreaDto.getPageNum();
              Page<SysArea> page = startPage(pageNum,pageSize);
              QueryWrapper<SysArea> wrapper = new QueryWrapper<SysArea>();
              IPage<SysArea> pageResult = sysAreaService.page(page,wrapper);
              List<SysArea> list = pageResult.getRecords();
              return Success(getPageResult(list,pageNum,pageSize,(int)pageResult.getTotal()));
         }catch (Exception e){
              logger.error("查询异常：===》" + e);
              return Error(e.getMessage());
         }
     }
     @ApiOperation(value = "数据详情")
     @GetMapping("/{id}")
     public Message getSysAreaInfo(@PathVariable("id") String id){
          try {
               SysArea sysArea = sysAreaService.getById(id);
               return Success(sysArea);
          }catch (Exception e){
               logger.error("查询异常：===》" + e);
               return Error(e.getMessage());
          }
     }

     @ApiOperation(value = "根据id更新")
     @PutMapping()
     public Message updatesysArea(@RequestBody SysAreaDto sysAreaDto){
           try{
               SysArea sysArea = BeanCopyUtil.convertBean(sysAreaDto, SysArea.class);
               sysAreaService.updateById(sysArea);
               return Success();
           }catch(Exception e){
               logger.error("更新异常：===》" + e);
               return Error(e.getMessage());
           }
     }
     @ApiOperation(value = "根据baseId逻辑删除")
     @DeleteMapping("/{id}")
     public Message updateByIsDelete(@PathVariable("id") String id){
           try{
                List<String> strings = StringUtil.splitStr(id, ",");
                return Success(sysAreaService.removeByIds(strings));
           }catch(Exception e){
               logger.error("删除异常：===》" + e);
               return Error(e.getMessage());
           }
     }
      @ApiOperation(value = "自定义查询列表-分页")
      @GetMapping("findSysAreaListPage")
      @ApiIgnore
    public Message findSysAreaListPage(SysAreaDto sysAreaDto) {
            try {
                    Integer pageSize = sysAreaDto.getPageSize();
                    Integer pageNum = sysAreaDto.getPageNum();
                    Page<SysArea> page = startPage(pageNum,pageSize);
                    SysArea sysArea = BeanCopyUtil.convertBean(sysAreaDto, SysArea.class);
                    List<SysArea> list = sysAreaService.findListPage(page,sysArea);
                    return Success( getPageResult(list,pageNum,pageSize,(int)page.getTotal()));
                }catch (Exception e){
                    logger.error("查询异常：===》" + e);
                    return Error(e.getMessage());
            }
      }

     @ApiOperation(value = "获取省市区")
     @GetMapping("getArea")
     public  Message getArea(SysAreaDto sysAreaDto){

         SysArea sysArea = BeanCopyUtil.convertBean(sysAreaDto, SysArea.class);
         List<SysArea> list = sysAreaService.findListPage(null,sysArea);
         return Success(list);
     }

    @ApiOperation(value = "获取省市区")
    @GetMapping("getId")
     public Message getId(){
         Map map= MapUtils.getAddress("117.186513","36.684032");
         SysAreaDto sysAreaDto=new  SysAreaDto();
         String privince=map.get("privince").toString();
         sysAreaDto.setBaseName(privince);
         SysArea sysArea = BeanCopyUtil.convertBean(sysAreaDto, SysArea.class);
         List<SysArea> list = sysAreaService.findListPage(null,sysArea);
         return Success(list);
     }
    @ApiOperation(value = "获取省市区")
    @GetMapping("getbyBaseName")
    public  Message getbyBaseNameFeign(@RequestParam(value = "baseName") String baseName, @RequestParam(value = "areaLevel") Integer areaLevel){
        SysArea sysArea=new SysArea();
        sysArea.setBaseName(baseName);
        sysArea.setAreaLevel(areaLevel);
        SysArea area = sysAreaService.findListPage(null,sysArea).get(0);
        return Success(area);
    }

    /**
     * 返回树结构的地区数据
     * @return
     */
    @GetMapping("/getAreaTree")
    public Message getAreaTree(){
        List<Tree<String>> areaTree = sysAreaService.getAreaTree();
        return Message.success(areaTree);
    }
}